2015-09-22(胡工).sql 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. SET ANSI_NULLS ON
  2. GO
  3. SET QUOTED_IDENTIFIER ON
  4. GO
  5. CREATE TABLE [dbo].[tb_ErpWageCommissionRecords](
  6. [ID] [int] IDENTITY(1,1) NOT NULL,
  7. [Wcr_EmployeeID] [nvarchar](20) NULL,
  8. [Wcr_CommissionScheme] [nvarchar](25) NULL,
  9. [Wcr_CurrentPerformance] [decimal](18, 2) NULL,
  10. [Wcr_PieceCommission] [decimal](18, 2) NULL,
  11. [Wcr_CommissionWages] [decimal](18, 2) NULL,
  12. [Wcr_PerformanceTimeStart] [datetime] NULL,
  13. [Wcr_PerformanceTimeEnd] [datetime] NULL,
  14. [Wcr_CreateDateTime] [datetime] NULL,
  15. [Wcr_CreateName] [nvarchar](20) NULL,
  16. CONSTRAINT [PK_tb_ErpWageCommissionRecords] PRIMARY KEY CLUSTERED
  17. (
  18. [ID] ASC
  19. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  20. ) ON [PRIMARY]
  21. GO
  22. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tb_ErpWageCommissionRecords', @level2type=N'COLUMN',@level2name=N'ID'
  23. GO
  24. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'员工编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tb_ErpWageCommissionRecords', @level2type=N'COLUMN',@level2name=N'Wcr_EmployeeID'
  25. GO
  26. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'提成方案编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tb_ErpWageCommissionRecords', @level2type=N'COLUMN',@level2name=N'Wcr_CommissionScheme'
  27. GO
  28. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'当前业绩' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tb_ErpWageCommissionRecords', @level2type=N'COLUMN',@level2name=N'Wcr_CurrentPerformance'
  29. GO
  30. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'计件提成' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tb_ErpWageCommissionRecords', @level2type=N'COLUMN',@level2name=N'Wcr_PieceCommission'
  31. GO
  32. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'提成工资' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tb_ErpWageCommissionRecords', @level2type=N'COLUMN',@level2name=N'Wcr_CommissionWages'
  33. GO
  34. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'业绩开始时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tb_ErpWageCommissionRecords', @level2type=N'COLUMN',@level2name=N'Wcr_PerformanceTimeStart'
  35. GO
  36. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'业绩结束时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tb_ErpWageCommissionRecords', @level2type=N'COLUMN',@level2name=N'Wcr_PerformanceTimeEnd'
  37. GO
  38. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'录入时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tb_ErpWageCommissionRecords', @level2type=N'COLUMN',@level2name=N'Wcr_CreateDateTime'
  39. GO
  40. EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'录入人' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tb_ErpWageCommissionRecords', @level2type=N'COLUMN',@level2name=N'Wcr__CreateName'
  41. GO
  42. EXEC sys.sp_addextendedproperty @name=N'tb_ErpWageCommissionRecords', @value=N'工资提成记录表' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tb_ErpWageCommissionRecords'
  43. GO
  44. IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = N'Vw_ErpWageCommissionRecords')
  45. BEGIN
  46. DROP VIEW [dbo].Vw_ErpWageCommissionRecords
  47. END
  48. GO
  49. create View Vw_ErpWageCommissionRecords
  50. as
  51. SELECT ID
  52. ,Wcr_EmployeeID as 员工编号
  53. ,(select [User_Name] from tb_ErpUser where Wcr_EmployeeID=User_EmployeeID) as 员工姓名
  54. ,Wcr_CommissionScheme as 提成方案编号
  55. ,(select Sc_ClassName from tb_ErpSystemCategory where Wcr_CommissionScheme=Sc_ClassCode) as 提成方案名称
  56. ,Wcr_CurrentPerformance as 当前业绩
  57. ,Wcr_PieceCommission as 计件提成
  58. ,Wcr_CommissionWages as 提成工资
  59. ,Wcr_PerformanceTimeStart as 业绩开始时间
  60. ,Wcr_PerformanceTimeEnd as 业绩结束时间
  61. ,Wcr_CreateDateTime as 录入时间
  62. ,Wcr_CreateName as 录入人编号
  63. ,(select [User_Name] from tb_ErpUser where Wcr_CreateName=User_EmployeeID) as 录入人名称
  64. FROM tb_ErpWageCommissionRecords
  65. GO
  66. IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = N'Vw_Customer_PaymentOrders')
  67. BEGIN
  68. DROP VIEW [dbo].Vw_Customer_PaymentOrders
  69. END
  70. GO
  71. create View Vw_Customer_PaymentOrders
  72. as
  73. select
  74. tb_ErpOrder.ID
  75. ,Ord_Number
  76. ,Ord_DividedShop
  77. ,Ord_Type
  78. ,Ord_OrderClass
  79. ,Ord_PhotographyCategory
  80. ,Ord_SeriesName
  81. ,Ord_SeriesPrice
  82. ,Ord_Class
  83. ,GP_OrderNumber
  84. ,GP_CustomerGroupID
  85. ,Cus_Name
  86. ,Cus_Name_py
  87. ,Cus_Telephone
  88. ,M_Cus_CustomerNumber
  89. ,(select stuff((select ','+ OrdPe_OrdersPerson from tb_ErpOrdersPerson where OrdPe_OrderNumber=Ord_Number for xml path('')),1,1,'')) as Ord_OrdersPersonID
  90. ,(select stuff((select ','+dbo.fn_CheckUserIDGetUserName( OrdPe_OrdersPerson) from tb_ErpOrdersPerson where OrdPe_OrderNumber=Ord_Number for xml path('')),1,1,'')) as Ord_OrdersPerson
  91. ,Ord_CreateDatetime
  92. ,( SELECT count(id) FROM tb_ErpOrderProductList where OPlist_OrderNumber=Ord_Number and OPlist_PickupStatus=0) as PickupStatusCount
  93. ,Ord_SinceOrderNumber
  94. from tb_ErpOrder
  95. left join tempTB_AggregationCustomer on Ord_Number=GP_OrderNumber
  96. GO
  97. IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = N'Vw_OrdersWagePaymentRecords')
  98. BEGIN
  99. DROP VIEW [dbo].Vw_OrdersWagePaymentRecords
  100. END
  101. GO
  102. create View Vw_OrdersWagePaymentRecords
  103. as
  104. SELECT
  105. tb_ErpPayment.ID,
  106. Pay_OrdNumber,
  107. Pay_AmountOf,
  108. Pay_OpenSingle,
  109. Pay_ThePayee,
  110. Pay_PaymentMethod,
  111. Pay_OrdersLocation,
  112. Pay_ReceivableProject,
  113. Pay_Remark,
  114. convert(varchar(10),Pay_CreateDatetime,120) as Pay_CreateDatetime,
  115. Pay_Category,
  116. Pay_TwoPinsCategory,
  117. dbo.fn_CheckUserIDGetUserName(Pay_OpenSingle) AS Pay_UserName,
  118. dbo.fn_CheckUserIDGetUserName(Pay_ThePayee) AS Pay_ThePayeeName,
  119. dbo.fn_GetClassCodeToName(Pay_PaymentMethod, Pay_PaymentMethod) AS Pay_PaymentMethodName,
  120. dbo.fn_GetClassCodeToName(Pay_TwoPinsCategory, Pay_TwoPinsCategory) AS Pay_TwoPinsCategoryName,
  121. Pay_FinancialAuditdPeople,
  122. Pay_FinancialAudit,
  123. dbo.fn_CheckUserIDGetUserName(Pay_FinancialAuditdPeople)AS Pay_FinancialAuditdPeopleName,
  124. Pay_ShootingName,
  125. Pay_Type,
  126. Ord_DividedShop,
  127. Ord_Type,
  128. Cus_Name as Ord_CustomerName1,
  129. (select Tsorder_Name from Vw_TwoSalesOrder where Pay_OrdNumber= Tsorder_Number) as Tsorder_Name,
  130. (select Tsorder_CustomerName from Vw_TwoSalesOrder where Pay_OrdNumber= Tsorder_Number) as Tsorder_CustomerName,
  131. (select Cus_Name from View_DressSaleRentalOrder where Pay_OrdNumber=Dsro_Number) as Cus_Name,
  132. Ord_CreateDatetime,
  133. Ord_SeriesName,
  134. Ord_PhotographyCategory
  135. ,Ord_SinceOrderNumber
  136. FROM tb_ErpPayment
  137. left join Vw_Customer_PaymentOrders on Pay_OrdNumber=Ord_Number
  138. GO
  139. IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = N'Vw_ErpPieceCommissionRecords')
  140. BEGIN
  141. DROP VIEW [dbo].Vw_ErpPieceCommissionRecords
  142. END
  143. GO
  144. create View Vw_ErpPieceCommissionRecords
  145. as
  146. SELECT tb_ErpPieceCommissionRecords.ID
  147. ,Pcr_OrderNumber
  148. ,Pcr_DigitalDivision
  149. ,Pcr_Date
  150. ,Pcr_CompletionContents
  151. ,Pcr_Quantity
  152. ,Pcr_CreateTime
  153. ,Pcr_EntryPeople
  154. ,Pcr_Type
  155. ,(select [User_Name] from tb_ErpUser where Pcr_DigitalDivision=User_EmployeeID) as Pcr_DigitalDivisionName
  156. ,[dbo].[fn_GetClassCodeToName](Pcr_CompletionContents,Pcr_CompletionContents) as Pcr_CompletionContentsName
  157. ,(select [User_Name] from tb_ErpUser where Pcr_EntryPeople=User_EmployeeID) as Pcr_EntryPeopleName
  158. ,Ord_Type
  159. ,Ord_Class
  160. ,(select Cus_Name from tempTB_AggregationCustomer where Pcr_OrderNumber=GP_OrderNumber) as Cus_Name
  161. ,(select Cus_Telephone from tempTB_AggregationCustomer where Pcr_OrderNumber=GP_OrderNumber) as Cus_Telephone
  162. ,Ord_SeriesName
  163. ,Ord_SeriesPrice
  164. ,Pcr_Quantity*(select Wcs_Percentage from tb_ErpWageCommissionSet where Pcr_CompletionContents=Wcs_TypeCode) as 总价格
  165. ,Ord_SinceOrderNumber
  166. FROM tb_ErpPieceCommissionRecords
  167. left join tb_ErpOrder on tb_ErpPieceCommissionRecords.Pcr_OrderNumber=tb_ErpOrder.Ord_Number
  168. GO
  169. IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = N'Vw_StaffPerformance_OrdersPerson')
  170. BEGIN
  171. DROP VIEW [dbo].Vw_StaffPerformance_OrdersPerson
  172. END
  173. GO
  174. create View Vw_StaffPerformance_OrdersPerson
  175. as
  176. SELECT
  177. tb_ErpPayment.ID
  178. ,Pay_OrdNumber as 订单号
  179. ,Pay_ShootingName as 拍摄阶段
  180. ,Pay_Category as 收款类别
  181. ,Pay_TwoPinsCategory as 二销类别编号
  182. ,dbo.fn_GetClassCodeToName(Pay_TwoPinsCategory, Pay_TwoPinsCategory) AS 二销类别名称
  183. ,Pay_AmountOf as 收款金额
  184. ,Pay_OpenSingle as 接单人编号
  185. , dbo.fn_CheckUserIDGetUserName(Pay_OpenSingle) AS 接单人名称
  186. ,Pay_ThePayee as 收款人编号
  187. ,dbo.fn_CheckUserIDGetUserName(Pay_ThePayee) AS 收款人名称
  188. ,Pay_PaymentMethod as 付款方式编号
  189. ,dbo.fn_GetClassCodeToName(Pay_PaymentMethod, Pay_PaymentMethod) AS 付款方式名称
  190. ,Pay_OrdersLocation as 接单地点
  191. ,Pay_ReceivableProject as 收款项目
  192. ,Pay_FinancialAudit as 审核状态
  193. ,Pay_FinancialAuditdPeople as 审核人
  194. ,Pay_Remark as 备注
  195. ,Pay_CreateDatetime as 收款时间
  196. ,Pay_Type as 收款类型
  197. ,(case Pay_Type
  198. when 0 then (select Cus_Name from tempTB_AggregationCustomer where Pay_OrdNumber=GP_OrderNumber)
  199. when 1 then (select Tsorder_CustomerName from Vw_TwoSalesOrder where Pay_OrdNumber=Tsorder_Number)
  200. when 2 then (select Cus_Name from View_DressSaleRentalOrder where Pay_OrdNumber=Dsro_Number) else '' end) as '客户名称'
  201. ,(case Pay_Type
  202. when 0 then (select Ord_PhotographyCategory from tb_ErpOrder where Pay_OrdNumber=Ord_Number)else '' end) as '套系类别'
  203. ,(case Pay_Type
  204. when 0 then (select Ord_SeriesName from tb_ErpOrder where Pay_OrdNumber=Ord_Number)else '' end) as '套系名称'
  205. ,(case Pay_Type
  206. when 0 then (select Ord_OrderClass from tb_ErpOrder where Pay_OrdNumber=Ord_Number)else '' end) as '订单类别'
  207. ,(case Pay_Type
  208. when 0 then (select Ord_SeriesPrice from tb_ErpOrder where Pay_OrdNumber=Ord_Number)
  209. when 1 then (select Tsorder_Money from Vw_TwoSalesOrder where Pay_OrdNumber=Tsorder_Number)
  210. when 2 then (select Dsro_Amount from View_DressSaleRentalOrder where Pay_OrdNumber=Dsro_Number) end) as '应收金额'
  211. ,(case Pay_Type
  212. when 0 then (dbo.fn_GetClassCodeToName(Pay_TwoPinsCategory, Pay_TwoPinsCategory))
  213. when 1 then (dbo.fn_GetClassCodeToName(Pay_TwoPinsCategory, Pay_TwoPinsCategory))
  214. when 2 then Pay_ReceivableProject end) as '项目名称'
  215. ,case when (select Cp_Proportion from tb_ErpCommissionPercentage where Cp_ProportionCode='MainStoreProportion') is null then '0' else (select Cp_Proportion from tb_ErpCommissionPercentage where Cp_ProportionCode='MainStoreProportion') end as '主门市比重'
  216. ,case when (select Cp_Proportion from tb_ErpCommissionPercentage where Cp_ProportionCode='DeputyStoreProportion') is null then '0' else (select Cp_Proportion from tb_ErpCommissionPercentage where Cp_ProportionCode='DeputyStoreProportion') end as '副门市比重'
  217. , ( case LEFT(Pay_OpenSingle,charindex(',',Pay_OpenSingle,1))
  218. when '' then Pay_OpenSingle
  219. else LEFT(Pay_OpenSingle,charindex(',',Pay_OpenSingle,1)-1) end)as '主门市'
  220. ,len(rtrim(ltrim(Pay_OpenSingle))) - len(rtrim(ltrim(replace(Pay_OpenSingle,',','')))) as '副门市个数'
  221. --, ( case Pay_Category
  222. -- when '后期收款' then ( len(rtrim(ltrim(Pay_OpenSingle))) - len(rtrim(ltrim(replace(Pay_OpenSingle,',','')))))
  223. -- else (select count(*) from tb_ErpOrdersPerson where OrdPe_OrderNumber=Pay_OrdNumber and OrdPe_Type='1') end) as '副门市个数'
  224. ,(case (len(rtrim(ltrim(Pay_OpenSingle))) - len(rtrim(ltrim(replace(Pay_OpenSingle,',','')))))
  225. when 0 then Pay_AmountOf
  226. else cast(Pay_AmountOf * case when (select Cp_Proportion from tb_ErpCommissionPercentage where Cp_ProportionCode='MainStoreProportion') is null then '0' else (select Cp_Proportion from tb_ErpCommissionPercentage where Cp_ProportionCode='MainStoreProportion') end*0.01 as numeric(9,2))
  227. end) as '主门市金额'
  228. ,(case (len(rtrim(ltrim(Pay_OpenSingle))) - len(rtrim(ltrim(replace(Pay_OpenSingle,',','')))))
  229. when 0 then 0
  230. else cast(Pay_AmountOf * case when (select Cp_Proportion from tb_ErpCommissionPercentage where Cp_ProportionCode='DeputyStoreProportion') is null then '0' else (select Cp_Proportion from tb_ErpCommissionPercentage where Cp_ProportionCode='DeputyStoreProportion') end*0.01/(len(rtrim(ltrim(Pay_OpenSingle))) - len(rtrim(ltrim(replace(Pay_OpenSingle,',',''))))) as numeric(9,2))
  231. end) as '副门市金额'
  232. ,(select top 1 Ordpg_ViceNumber from tb_ErpOrdersPhotography where Ordpg_Number=Pay_OrdNumber) as 副订单号
  233. ,(case (select Ord_Type from tb_ErpOrder where Ord_Number=Pay_OrdNumber)
  234. when 0 then (select max(Ordpg_PhotographyTime) from tb_ErpOrdersPhotography where Ordpg_Number=Pay_OrdNumber)
  235. when 1 then (select Ordpg_PhotographyTime from tb_ErpOrdersPhotography where Ordpg_ViceNumber=(select top 1 Ordpg_ViceNumber from tb_ErpOrdersPhotography where Ordpg_Number=Pay_OrdNumber) )
  236. when 2 then (select max(Ordpg_PhotographyTime) from tb_ErpOrdersPhotography where Ordpg_Number=Pay_OrdNumber)
  237. else '' end) as 最后拍摄时间
  238. ,(case (select Ord_Type from tb_ErpOrder where Ord_Number=Pay_OrdNumber)
  239. when 0 then (select count(id) as id from tb_ErpOrdersPhotography where Ordpg_Number=Pay_OrdNumber and (Ordpg_PhotographyStatus=0 or Ordpg_PhotographyStatus=2))
  240. when 1 then (select count(id) as id from tb_ErpOrdersPhotography where Ordpg_ViceNumber=(select top 1 Ordpg_ViceNumber from tb_ErpOrdersPhotography where Ordpg_Number=Pay_OrdNumber) and (Ordpg_PhotographyStatus=0 or Ordpg_PhotographyStatus=2))
  241. when 2 then (select count(id) as id from tb_ErpOrdersPhotography where Ordpg_Number=Pay_OrdNumber and (Ordpg_PhotographyStatus=0 or Ordpg_PhotographyStatus=2))
  242. else '' end) as 未拍个数
  243. ,(case (select Ord_Type from tb_ErpOrder where Ord_Number=Pay_OrdNumber)
  244. when 0 then (select Ordv_FilmSelectionStatus from tb_ErpOrderDigital where Ordv_Number=Pay_OrdNumber)
  245. when 1 then (select Ordv_FilmSelectionStatus from tb_ErpOrderDigital where Ordv_ViceNumber=(select top 1 Ordpg_ViceNumber from tb_ErpOrdersPhotography where Ordpg_Number=Pay_OrdNumber))
  246. when 2 then (select Ordv_FilmSelectionStatus from tb_ErpOrderDigital where Ordv_Number=Pay_OrdNumber)
  247. else '' end) as 选片状态
  248. ,(case (select Ord_Type from tb_ErpOrder where Ord_Number=Pay_OrdNumber)
  249. when 0 then (select Ordv_FilmSelectionTime from tb_ErpOrderDigital where Ordv_Number=Pay_OrdNumber)
  250. when 1 then (select Ordv_FilmSelectionTime from tb_ErpOrderDigital where Ordv_ViceNumber=(select top 1 Ordpg_ViceNumber from tb_ErpOrdersPhotography where Ordpg_Number=Pay_OrdNumber))
  251. when 2 then (select Ordv_FilmSelectionTime from tb_ErpOrderDigital where Ordv_Number=Pay_OrdNumber)
  252. else '' end) as 选片时间
  253. ,(case when (select Count(*) from (select [OPlist_PickupStatus] from [tb_ErpOrderProductList] where [OPlist_ViceNumber]=(select top 1 Ordpg_ViceNumber from tb_ErpOrdersPhotography where Ordpg_Number=Pay_OrdNumber) and OPlist_Type = '2' and [OPlist_PickupStatus] = '0') as ta)>0 then '未取' else 'OK' end) AS 取件状态
  254. ,(select top 1 OPlist_PickupTime from tb_ErpOrderProductList where OPlist_ViceNumber = (select top 1 Ordpg_ViceNumber from tb_ErpOrdersPhotography where Ordpg_Number=Pay_OrdNumber) and OPlist_Type = '2' and OPlist_PickupTime is not null order by OPlist_PickupTime) AS 取件日期
  255. ,Ord_CustomerSource as 客户来源
  256. ,(select sum(Pay_AmountOf) from tb_ErpPayment where Ord_Number=Pay_OrdNumber and Pay_Category!='后期收款') as 前期实收金额
  257. ,Ord_SinceOrderNumber as 自定义订单号
  258. FROM tb_ErpPayment
  259. left join tb_ErpOrder on Pay_OrdNumber=Ord_Number
  260. GO
  261. IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = N'Vw_StaffPerformance_Photograph')
  262. BEGIN
  263. DROP VIEW [dbo].Vw_StaffPerformance_Photograph
  264. END
  265. GO
  266. create View Vw_StaffPerformance_Photograph
  267. as
  268. SELECT
  269. Ordv_Number as 主订单
  270. ,Ordv_ViceNumber as 副订单
  271. ,(select Ord_Type from tb_ErpOrder where Ordv_Number=Ord_Number) as 订单类型
  272. ,(case (select Ord_Type from tb_ErpOrder where Ordv_Number=Ord_Number)
  273. when 0 then (select stuff((select ','+ Ordpg_Sights from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  274. when 1 then (select stuff((select ','+ Ordpg_Sights from tb_ErpOrdersPhotography where Ordv_ViceNumber=Ordpg_ViceNumber for xml path('')),1,1,''))
  275. when 2 then (select stuff((select ','+ Ordpg_Sights from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  276. else '' end) as 拍摄名称
  277. ,(case (select Ord_Type from tb_ErpOrder where Ordv_Number=Ord_Number)
  278. when 0 then (select max(Ordpg_PhotographyTime) from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number)
  279. when 1 then (select max(Ordpg_PhotographyTime) from tb_ErpOrdersPhotography where Ordv_ViceNumber=Ordpg_ViceNumber)
  280. when 2 then (select max(Ordpg_PhotographyTime) from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number)
  281. else '' end) as 最后拍摄时间
  282. ,(case (select Ord_Type from tb_ErpOrder where Ordv_Number=Ord_Number)
  283. when 0 then (select count(id) as id from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number and (Ordpg_PhotographyStatus=0 or Ordpg_PhotographyStatus=2))
  284. when 1 then (select count(id) as id from tb_ErpOrdersPhotography where Ordv_ViceNumber=Ordpg_ViceNumber and (Ordpg_PhotographyStatus=0 or Ordpg_PhotographyStatus=2))
  285. when 2 then (select count(id) as id from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number and (Ordpg_PhotographyStatus=0 or Ordpg_PhotographyStatus=2))
  286. else '' end) as 未拍个数
  287. ,(case Ordv_FilmSelectionStatus when 1 then 'OK' else '未选' end) as 选片状态
  288. ,Ordv_FilmSelectionTime as 选片时间
  289. ,(case when (select Count(*) from (select [OPlist_PickupStatus] from [tb_ErpOrderProductList] where [OPlist_ViceNumber]=Ordv_ViceNumber and OPlist_Type = '2' and [OPlist_PickupStatus] = '0') as ta)>0 then '未取' else 'OK' end) AS 取件状态
  290. ,(select top 1 OPlist_PickupTime from tb_ErpOrderProductList where OPlist_ViceNumber = Ordv_ViceNumber and OPlist_Type = '2' and OPlist_PickupTime is not null order by OPlist_PickupTime) AS 取件日期
  291. ,(case (select Ord_Type from tb_ErpOrder where Ordv_Number=Ord_Number)
  292. when 0 then (select count(*) as [count] from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number and Ordpg_SightsLevel='BEBBBCADDEBFJDFFC')
  293. when 1 then (select count(*) as [count] from tb_ErpOrdersPhotography where Ordpg_ViceNumber=Ordv_ViceNumber and Ordpg_SightsLevel='BEBBBCADDEBFJDFFC')
  294. when 2 then (select count(*) as [count] from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number and Ordpg_SightsLevel='BEBBBCADDEBFJDFFC')
  295. else '' end) as '景点一级个数'
  296. ,(case (select Ord_Type from tb_ErpOrder where Ordv_Number=Ord_Number)
  297. when 0 then (select count(*) as [count] from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number and Ordpg_SightsLevel='BEBBBCADEGBGAFFJC')
  298. when 1 then (select count(*) as [count] from tb_ErpOrdersPhotography where Ordpg_ViceNumber=Ordv_ViceNumber and Ordpg_SightsLevel='BEBBBCADEGBGAFFJC')
  299. when 2 then (select count(*) as [count] from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number and Ordpg_SightsLevel='BEBBBCADEGBGAFFJC')
  300. else '' end) as '景点二级个数'
  301. ,(case (select Ord_Type from tb_ErpOrder where Ordv_Number=Ord_Number)
  302. when 0 then (select count(*) as [count] from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number and Ordpg_SightsLevel='BEBBBCADAFBHBCHCI')
  303. when 1 then (select count(*) as [count] from tb_ErpOrdersPhotography where Ordpg_ViceNumber=Ordv_ViceNumber and Ordpg_SightsLevel='BEBBBCADAFBHBCHCI')
  304. when 2 then (select count(*) as [count] from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number and Ordpg_SightsLevel='BEBBBCADAFBHBCHCI')
  305. else '' end) as '景点三级个数'
  306. ,(case (select Ord_Type from tb_ErpOrder where Ordv_Number=Ord_Number)
  307. when 0 then (select stuff((select ','+ Ordpg_Photographer from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  308. when 1 then (select stuff((select ','+ Ordpg_Photographer from tb_ErpOrdersPhotography where Ordv_ViceNumber=Ordpg_ViceNumber for xml path('')),1,1,''))
  309. when 2 then (select stuff((select ','+ Ordpg_Photographer from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  310. else '' end) as 主摄影师ID
  311. ,(case (select Ord_Type from tb_ErpOrder where Ordv_Number=Ord_Number)
  312. when 0 then (select stuff((select ','+ dbo.fn_CheckUserIDGetUserName(Ordpg_Photographer) from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  313. when 1 then (select stuff((select ','+ dbo.fn_CheckUserIDGetUserName(Ordpg_Photographer) from tb_ErpOrdersPhotography where Ordv_ViceNumber=Ordpg_ViceNumber for xml path('')),1,1,''))
  314. when 2 then (select stuff((select ','+ dbo.fn_CheckUserIDGetUserName(Ordpg_Photographer) from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  315. else '' end) as 主摄影师名称
  316. ,(case (select Ord_Type from tb_ErpOrder where Ordv_Number=Ord_Number)
  317. when 0 then (select stuff((select ','+ Ordpg_PhotographyAssistant from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  318. when 1 then (select stuff((select ','+ Ordpg_PhotographyAssistant from tb_ErpOrdersPhotography where Ordv_ViceNumber=Ordpg_ViceNumber for xml path('')),1,1,''))
  319. when 2 then (select stuff((select ','+ Ordpg_PhotographyAssistant from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  320. else '' end) as 摄影助理ID
  321. ,(case (select Ord_Type from tb_ErpOrder where Ordv_Number=Ord_Number)
  322. when 0 then (select stuff((select ','+ dbo.fn_CheckUserIDGetUserName(Ordpg_PhotographyAssistant) from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  323. when 1 then (select stuff((select ','+ dbo.fn_CheckUserIDGetUserName(Ordpg_PhotographyAssistant) from tb_ErpOrdersPhotography where Ordv_ViceNumber=Ordpg_ViceNumber for xml path('')),1,1,''))
  324. when 2 then (select stuff((select ','+ dbo.fn_CheckUserIDGetUserName(Ordpg_PhotographyAssistant) from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  325. else '' end) as 摄影助理名称
  326. ,(case (select Ord_Type from tb_ErpOrder where Ordv_Number=Ord_Number)
  327. when 0 then (select stuff((select ','+ Ordpg_MakeupArtist from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  328. when 1 then (select stuff((select ','+ Ordpg_MakeupArtist from tb_ErpOrdersPhotography where Ordv_ViceNumber=Ordpg_ViceNumber for xml path('')),1,1,''))
  329. when 2 then (select stuff((select ','+ Ordpg_MakeupArtist from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  330. else '' end) as 主化妆ID
  331. ,(case (select Ord_Type from tb_ErpOrder where Ordv_Number=Ord_Number)
  332. when 0 then (select stuff((select ','+ dbo.fn_CheckUserIDGetUserName(Ordpg_MakeupArtist) from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  333. when 1 then (select stuff((select ','+ dbo.fn_CheckUserIDGetUserName(Ordpg_MakeupArtist) from tb_ErpOrdersPhotography where Ordv_ViceNumber=Ordpg_ViceNumber for xml path('')),1,1,''))
  334. when 2 then (select stuff((select ','+ dbo.fn_CheckUserIDGetUserName(Ordpg_MakeupArtist) from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  335. else '' end) as 主化妆名称
  336. ,(case (select Ord_Type from tb_ErpOrder where Ordv_Number=Ord_Number)
  337. when 0 then (select stuff((select ','+ Ordpg_MakeupAssistant from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  338. when 1 then (select stuff((select ','+ Ordpg_MakeupAssistant from tb_ErpOrdersPhotography where Ordv_ViceNumber=Ordpg_ViceNumber for xml path('')),1,1,''))
  339. when 2 then (select stuff((select ','+ Ordpg_MakeupAssistant from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  340. else '' end) as 化妆助理ID
  341. ,(case (select Ord_Type from tb_ErpOrder where Ordv_Number=Ord_Number)
  342. when 0 then (select stuff((select ','+ dbo.fn_CheckUserIDGetUserName(Ordpg_MakeupAssistant) from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  343. when 1 then (select stuff((select ','+ dbo.fn_CheckUserIDGetUserName(Ordpg_MakeupAssistant) from tb_ErpOrdersPhotography where Ordv_ViceNumber=Ordpg_ViceNumber for xml path('')),1,1,''))
  344. when 2 then (select stuff((select ','+ dbo.fn_CheckUserIDGetUserName(Ordpg_MakeupAssistant) from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  345. else '' end) as 化妆助理名称
  346. ,(case (select Ord_Type from tb_ErpOrder where Ordv_Number=Ord_Number)
  347. when 0 then (select stuff((select ','+ Ordpg_BootDivision from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  348. when 1 then (select stuff((select ','+ Ordpg_BootDivision from tb_ErpOrdersPhotography where Ordv_ViceNumber=Ordpg_ViceNumber for xml path('')),1,1,''))
  349. when 2 then (select stuff((select ','+ Ordpg_BootDivision from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  350. else '' end) as 引导师ID
  351. ,(case (select Ord_Type from tb_ErpOrder where Ordv_Number=Ord_Number)
  352. when 0 then (select stuff((select ','+ dbo.fn_CheckUserIDGetUserName(Ordpg_BootDivision) from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  353. when 1 then (select stuff((select ','+ dbo.fn_CheckUserIDGetUserName(Ordpg_BootDivision) from tb_ErpOrdersPhotography where Ordv_ViceNumber=Ordpg_ViceNumber for xml path('')),1,1,''))
  354. when 2 then (select stuff((select ','+ dbo.fn_CheckUserIDGetUserName(Ordpg_BootDivision) from tb_ErpOrdersPhotography where Ordpg_Number=Ordv_Number for xml path('')),1,1,''))
  355. else '' end) as 引导师名称
  356. ,Ordv_EarlyRepairName as '初修师ID'
  357. ,dbo.fn_CheckUserIDGetUserName(Ordv_EarlyRepairName) as '初修师'
  358. ,Ordv_RefinementName as '精修师ID'
  359. ,dbo.fn_CheckUserIDGetUserName(Ordv_RefinementName) as '精修师'
  360. ,Ordv_DesignerName as '设计师ID'
  361. ,dbo.fn_CheckUserIDGetUserName(Ordv_DesignerName) as '设计师'
  362. ,Vw_StaffPerformance_OrdersPerson.ID
  363. ,订单号
  364. ,拍摄阶段
  365. ,收款类别
  366. ,二销类别编号
  367. ,二销类别名称
  368. ,收款金额
  369. ,接单人编号
  370. ,接单人名称
  371. ,收款人编号
  372. ,收款人名称
  373. ,付款方式编号
  374. ,付款方式名称
  375. ,接单地点
  376. ,收款项目
  377. ,审核状态
  378. ,审核人
  379. ,备注
  380. ,收款时间
  381. ,收款类型
  382. ,客户名称
  383. ,套系类别
  384. ,套系名称
  385. ,订单类别
  386. ,应收金额
  387. ,项目名称
  388. ,客户来源
  389. ,自定义订单号
  390. FROM tb_ErpOrderDigital
  391. left join Vw_StaffPerformance_OrdersPerson on Ordv_Number=订单号
  392. where 订单号 is not null
  393. GO